<template>
	<view>
		<view class="container">
			<view class="friendly">
				<image v-if="isSuccess" src="@/static/chenggong.png" mode="widthFix" style="width: 112rpx;height: 112rpx;"></image>
				<image v-if="isFail || isCancel" src="@/static/close.jpg" mode="widthFix" style="width: 112rpx;height: 112rpx;"></image>
			</view>
			<view class="content">
				
				<view class="status-box">
					<view style="font-weight: 600;font-size: 38rpx;">
						<text v-if="isSuccess">支付成功</text>
						<text v-if="isFail || isCancel">支付失败</text>
					</view>
					<block v-if="isLoading">
						<view>支付中...</view>
					</block>
					<block v-if="isSuccess">
						<view style="margin-top: 30rpx">可以在个人中心“我的订单”查看</view>
					</block>
					<block v-if="isCancel">
						<view style="margin-top: 30rpx">非常抱歉，支付未成功</view>
					</block>
					<block v-if="isFail">
						<view style="margin-top: 30rpx">非常抱歉，支付未成功</view>
					</block>
					
				</view>
				
				<view class="flex flex-jc-c flex-dir-c flex-ai-c">
					<block v-if="isFail || isCancel">
						<view class="btn" style="background: #07C160;" @tap="toPayApp">
							<text>重新支付</text>	
						</view>
					</block>
					<view class="btn" v-if="isSuccess">
						<text @tap="lookOrder">查看订单</text>	
					</view>
					<!-- #07C160 -->
					<button style="margin-top: 30rpx;" type="default" @tap="skip"><text style="color: #000">返回首页</text></button>
				</view>
				<block v-if="ischeckProfit">
					<image @click="show = true" src="https://xy.outletds.com/uploads/icon/cash.png" style="height: 240rpx;width: 685rpx;"></image>
				</block>
				
			</view>
		</view>
		<!-- https://xy.outletds.com/uploads/icon/cash.png -->
		<!-- <u-popup v-model="isShow" mode="center" border-radius="25">
			<view style="width: 90vw;border-radius: 50%;padding: 30rpx 0 50rpx 0;">
				<view class="center-text" style="margin: 30rpx 0;font-weight: bold;">请设置支付密码</view>
				<u-code-input mode="line" :maxlength="4" @finish="finish"></u-code-input>
			</view>
		</u-popup>

		<u-popup v-model="payment" mode="center" border-radius="25">
			<view style="width: 90vw;border-radius: 50%;padding: 30rpx 0 50rpx 0;">
				<view class="center-text" style="margin: 30rpx 0;font-weight: bold;">余额支付</view>
				<u-code-input :maxlength="4" mode="line" @finish="balancePay"></u-code-input>
			</view>
		</u-popup>

		<u-popup v-model="payType" mode="bottom" border-radius="25">
			<view style="background: #fff;width: 100%;line-height: 100rpx;" class="flex flex-dir-c flex-jc-c flex-ai-c">
				<view @click="eventPay('wx')">微信支付</view>
				<view @click="eventPay('ye')">余额支付</view>
			</view>
		</u-popup> -->
	
		<activity @rejectProfit="rejectProfit" :rebateMoney="rebate_money" :show='show' @receive="receive" />
	</view>
</template>

<script>
	import user from '@/api2/user.js';
	import activity from '@/components/activity.vue';
	let timer = null
	let paytimer = null
	export default {
		components: {activity},
		data() {
			return {
				rebate_money: 0,
				show: false,
				payType: false,
				isShow: false,
				payment: false,
				isLoading: false,
				//支付中
				isSuccess: false,
				//成功
				isFail: false,
				//失败
				isCancel: false,
				//取消
				status: 0 ,// 0: 已取消(未付)，1：已支付，2：支付失败
				order_id: 0,
				id: null,
				ischeckProfit: false
			};
		},
		onLoad(op) {
			if (!op.order_id) {
				return this.showMsg('无效的支付请求，请返回商户重试');
			}
			this.eventBindOrder(op.order_id);
			this.order_id = op.order_id
		},
		onUnload() {
			clearInterval(timer)
			clearInterval(paytimer)
			paytimer = null
			timer = null
		},
		methods: {
			//唤起支付
			async pay(id) {
				let _this = this;
				this.isLoading = true
				const res = await user.wxPay({
					//获取订单信息
					order_id: id
				})
				if (res.code == 200) {
					let payInfo = res.data.payRes;
					uni.requestPayment({
						timeStamp: payInfo.timeStamp,
						nonceStr: payInfo.nonceStr,
						package: payInfo.package,
						signType: payInfo.signType,
						paySign: payInfo.paySign,
						success(res2) {
							paytimer = setInterval(() => {
								_this.checkOrderPayState()
							},2000)
						},
						fail(err2) {
							if (err2.errMsg === 'requestPayment:fail cancel') {
								_this.isLoading = false
								_this.isCancel = true
								_this.status = 0
								_this.showMsg('您已取消支付');
							} else {
								_this.isLoading = false
								_this.isFail = true
								_this.status = 2
							}
						}
					});
				} else {
					_this.isLoading = false
					_this.isFail = true
					_this.status = 2
				}
			},
			//检查订单是否支付
			async checkOrderPayState() {
				let _this = this;
				const res = await user.checkOrderPayState({
					id: this.order_id
				})
				if(res.data){
					_this.isLoading = false
					_this.status = 1
					_this.isSuccess = true
					_this.showMsg('支付成功');
					clearInterval(paytimer)
					paytimer = null
					timer = setInterval(() => {
						_this.checkProfit()
					},2000)
				}
			},
			//检查权益(是否弹窗）
			async checkProfit() {
				const res = await user.checkProfit()
				if(res.data.show_flag === 1) {
					this.ischeckProfit = true
					this.rebate_money = res.data.rebate_money
					this.id = res.data.id
					clearInterval(timer)
					timer = null
				}
			},
			async rejectProfit() {
				const res = await user.rejectProfit({
					id: this.id
				})
				if(res.code == 200) {
					this.show = false
					this.ischeckProfit = false
				}
			},
			async receive() {
				const res = await user.acceptProfit({
					id: this.id
				})
				if(res.code == 200) {
					uni.showToast({
						title: '领取成功',
						icon: 'none',
						duration: 2000,
						success() {
							this.show = false
							uni.navigateTo({
								url: '/pages/me/line_up_order' 
							})
						}
					})
				}
			},
			skipHd(){
				uni.navigateTo({
					url: '/pages/me/welfare'
				})
			},
			skip(){
				uni.reLaunch({
					url: '/pages/index/index?index=2'
				})
			},
			eventPay(type) {
				switch (type) {
					case 'wx':
						this.pay(this.order_id)
						break
					case 'ye':
						this.recharge()
						break
				}
			},
			//余额支付
			async balancePay() {
				uni.showToast({
					title: '支付成功',
					icon: 'none'
				})
			},
			//设置支付密码
			async finish() {
				uni.showToast({
					title: '设置成功',
					icon: 'none'
				})
				if (true) {
					this.isShow = false
				}
			},
			//判断是否是否设置支付密码
			async recharge() {
				if (false) {
					//余额支付
					this.payment = true
				} else {
					uni.showToast({
						title: '未设置支付密码，请设置支付密码！',
						icon: 'none'
					})
					this.isShow = true
				}
			},

			lookOrder() {
				uni.reLaunch({
					url: '/pages/index/index?index=1'
				});
			},

			showMsg(msg, success = () => {}) {
				uni.showToast({
					title: msg,
					icon: 'none',
					success: success
				});
			},

			//绑定订单
			async eventBindOrder(id) {
				let _this = this;
				const res = await user.bindOrder({
					order_id: id
				})
				if(res.code == 200) {
					_this.pay(id);
				}else if(res.code == 9001) {
					uni.redirectTo({
						url: `/pages/login/index?order_id=${id}`
					})
				}
			},

			toPayApp() {
				this.pay(this.order_id)
			},

			launchAppError() {
				uni.exitMiniProgram()
			}
		},
		options: {
			styleIsolation: 'shared'
		},
	};
</script>
<style>
	page{
		height: 100vh;
		background-color: #fff;
	}
</style>
<style>
	/deep/.u-line {
		display: none;
	}
	/deep/.u-model__title{
		display: none;
	}
	/deep/.u-popup__content {
		background: none !important;
	}
	/deep/.u-model{
		background: none !important;
	}
	/deep/.u-mode-center-box {
		background: none !important;
	}
	button {
		font-size: 28rpx;
		font-weight: 300;
		color: #999999;
		padding: 0;
		background: none !important;
		height: 90rpx;
		border-radius: 45rpx;
		line-height: 90rpx;
		border: 1rpx solid #999999;
		width: 384rpx;
	}
	
	button::after {
		border: none;
	}
	.container{
		height: 100%;
		border-top: 1rpx solid #fff;
	}
	.button-hover {
		color: rgba(0, 0, 0, 0.6);
		background-color: #fff;
	}

	.friendly {
		margin: 280rpx auto 0 auto;
		text-align: center;
	}

	image {
		width: 300rpx;
	}

	.content {
		padding: 0 40rpx;
		color: #666;
	}

	.status-box {
		padding: 60rpx 0 135rpx 0;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.pay-button {
		color: #333333;
	}

	.btn {
		margin-top: 40rpx;
		text-align: center;
		width: 384rpx;
		height: 90rpx;
		background: #FCB527;
		border-radius: 45rpx;
		line-height: 90rpx;
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: 400;
	}

	.btn2 text {
		padding: 10rpx 20rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #FFFFFF;
	}

	.pay-icon {
		margin-bottom: 30rpx;
	}
</style>